<script src="../util/ajax.js"></script>
<template>
  <el-container>
    <el-header>
      <h1>PWZMusic 音乐下载</h1>
    </el-header>
    <el-main>
      <el-input
        placeholder="请输入歌曲或歌手名称"
        class="input-with-select"
        v-model="keyWord"
        style="width: 60%"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="search()"
        ></el-button>
      </el-input>
      <br /><br />
      <div style="width: 60%;margin-left: 20%;">
        <el-radio-group
          v-model="platform"
          style="width: 90%;text-align: center"
        >
          <el-radio :label="'kg'">酷狗</el-radio>
          <el-radio :label="'kw'">酷我</el-radio>
          <el-radio :label="'tx'">QQ</el-radio>
          <el-radio :label="'mg'">咪咕</el-radio>
<!--          <el-radio :label="'wy'">网易</el-radio>-->
        </el-radio-group>
        <el-link
          type="danger"
          :underline="false"
          style="width: 10%;"
          @click="helpDialogVisible = true"
        >
          下载帮助
        </el-link>
      </div>
      <br />
      <el-table
        :data="tableData"
        border
        :max-height="650"
        style="margin-left:20%;width: 60%;"
        id="dataTable"
      >
        <el-table-column type="index" label="序号" width="100px" align="center">
        </el-table-column>
        <el-table-column
          prop="songName"
          label="歌名"
          width="calc(25%)"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="singerName"
          label="歌手"
          width="calc(20%)"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="albumName"
          label="专辑"
          width="calc(20%)"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="types"
          label="音质"
          width="calc(25%)"
          align="center"
        >
          <template slot-scope="scope">
            <a
              :download="scope.row.fileName"
              class="a-tag"
              v-for="type in scope.row.types"
              v-bind:key="type.types"
              title="点击获取下载信息"
              @click="getSongInfo(scope.row, type.hash, type.type)"
            >
              <el-tag :type="type.tagType" style="margin-left: 10px;"
              >{{ type.type }}
              </el-tag>
            </a>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalRows"
      >
        >
      </el-pagination>
    </el-main>
    <el-footer>
      注意:所有资源和数据均来自于网络，若有侵权，请<a
      href="mailto:zbh1309766078@126.com"
    >联系我</a
    >删除。
    </el-footer>
    <el-dialog title="下载帮助" :visible.sync="helpDialogVisible" width="40%">
      <h4>
        点击音质标签按钮，复制下载链接，然后打开下载工具（迅雷等），新建任务，粘贴下载链接下载即可，如果下载完成后的文件名与给出的文件名不一致，点击复制文件名，然后将下载得文件重命名为复制的文件名
      </h4>
      <h4 style="color: red">
        注意：由于服务器不稳定，偶尔会出现所有歌曲不能下载的问题，请过段时间再来下载！
      </h4>
    </el-dialog>
    <el-dialog title="下载" :visible.sync="downloadDialogVisible" width="40%">
      <el-form label-width="80px" size="mini">
        <el-form-item label="歌名"
        ><span>{{ songName }}</span></el-form-item
        >
        <el-form-item label="歌手"
        ><span>{{ singerName }}</span></el-form-item
        >
        <el-form-item label="文件名">
          <span>{{ fileName }}</span>
          <el-link
            v-clipboard:copy="fileName"
            v-clipboard:success="onCopy"
            :underline="false"
            v-clipboard:error="onError"
            type="primary"
            style="margin-left: 20px;"
          >复制
          </el-link>
        </el-form-item>
        <el-form-item label="下载地址">
          <span>{{ downloadUrl }}</span>
          <el-link
            v-clipboard:copy="downloadUrl"
            v-clipboard:success="onCopy"
            :underline="false"
            v-clipboard:error="onError"
            type="primary"
            style="margin-left: 20px;"
          >复制
          </el-link>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-container>
</template>

<script>
import request from "@/util/request";
import getAjax from "@/util/ajax";

export default {
  name: "IndexPage",
  data() {
    return {
      platform: "kg",
      tableData: [],
      keyWord: "",
      totalRows: 0,
      pageSize: 10,
      currentPage: 1,
      helpDialogVisible: false,
      downloadDialogVisible: false,
      songName: "",
      singerName: "",
      fileName: "",
      type: "",
      downloadUrl: ""
    };
  },
  methods: {
    async search() {
      /*request.search(
        this,
        this.platform,
        this.keyWord,
        this.currentPage,
        this.pageSize
      );*/
      let ajax = getAjax();
      let resp = await ajax.get("/api/search", {
        params: {
          keyword: this.keyWord,
          platform: this.platform,
          pageNo: this.currentPage,
          pageSize: this.pageSize
        }
      });
      if (resp.data.code === 200 && resp.data.data) {
        this.tableData = resp.data.data;
        this.totalRows = resp.data.total;
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      request.search(
        this,
        this.platform,
        this.keyWord,
        this.currentPage,
        this.pageSize
      );
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      request.search(
        this,
        this.platform,
        this.keyWord,
        this.currentPage,
        this.pageSize
      );
    },
    getSongInfo(songInfo, hash, type) {
      request.getSongUrl(this, this.platform, songInfo, hash, type);
    },
    onCopy(e) {
      this.$message.success("内容已复制到剪切板！");
    },
    onError(e) {
      this.$message.error("抱歉，复制失败！");
    }
  }
};
</script>

<style scoped>
.el-header h1 {
  background: linear-gradient(to right, red, blue);
  -webkit-background-clip: text;
  color: transparent;
}

.el-header,
.el-footer {
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-main {
  color: #333;
  text-align: center;
  height: 90%;
}

.el-pagination {
  text-align: right;
  margin-top: 20px;
  margin-right: 20%;
}

.a-tag {
  cursor: pointer;
}

body > .el-container {
  margin-bottom: 40px;
}
</style>
